package com.activiti.z_six.service.manager;

import com.activiti.z_six.entity.taskAssignee.AssigneeUserEntity;
import com.activiti.z_six.mapper.taskAssigneeMapper.AssigneeUserMapper;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@Component
@RequiredArgsConstructor
public class ProcessDefManager {
    @Autowired
    private AssigneeUserMapper assigneeUserMapper;

    /**
     *配置某个任务节点处理人
     * @param assigneeList
     * @param usertaskid
     * @return
     */
    public String setAssigneeUser(String assigneeList, String usertaskid,String ruleName){
        JSONArray userlist = JSONObject.parseArray(assigneeList);
        List<AssigneeUserEntity> assigneeUserEntityList = new ArrayList<>();
        for (int i = 0; i < userlist.size(); i++) {
            JSONObject json = JSONObject.parseObject(JSON.toJSONString(userlist.get(i)));
            AssigneeUserEntity assigneeUserEntity = new AssigneeUserEntity();
            if(SystemConfig.IsNullOrEmpty(json.getString("username"))){
                assigneeUserEntity.setUsername(json.getString("id"));
            }
            else {
                assigneeUserEntity.setUsername(json.getString("username"));
            }
            assigneeUserEntity.setUsertaskid(usertaskid);
            assigneeUserEntity.setRuleName(ruleName);
            assigneeUserEntity.setId(UUID.randomUUID().toString());
            assigneeUserEntityList.add(assigneeUserEntity);
        }
        assigneeUserMapper.setAssigneeUser(assigneeUserEntityList);

        return "执行成功";
    }
    /**
     * 删除节点接收人
     * @param usertaskid
     * @return
     */
    public String deleteAssigneeUserByTaskid(String usertaskid){
        assigneeUserMapper.deleteAssigneeUserByTaskid(usertaskid);
        return "执行成功";
    }
}
